﻿/* 2013/6/11 */
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Cosmos.IO.FileSystemPath.Searcher
{
    /// <summary>
    /// Generates paths by specifying search condition.
    /// Searchers file or directory by checking whether
    /// the name matches regular expression.
    /// </summary>
    public class RegexFilePathSearcher : FileSystemInfoPathSearcher
    {
        protected Regex regex = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchPattern"></param>
        /// <param name="directoryPath"></param>
        /// <param name="containDirectory"></param>
        /// <param name="invertMatch"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public override string[] Search(string searchPattern, string directoryPath, 
            SearchFileDirectoryOption searchFileDirectoryOption, bool invertMatch)
        {
            return base.Search(searchPattern, directoryPath, searchFileDirectoryOption, invertMatch);
        }

        protected override void Prepare(string searchPattern)
        {
            regex = new Regex(searchPattern);
        }

        protected override bool Match(FileSystemInfo info)
        {
            string name = info.Name;
            if (name != null)
            {
                if (regex == null)
                {
                    return true;
                }

                return regex.IsMatch(name);
            }
            return false;
        }
    }
}
